package com.ddyx.rest;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.ddyx.service.IDeviceService;

/**
 * @ClassName: DeviceController.java
 * @Module: 设备管理web模块
 * @Description: 
 * 
 * @author: QingGuan.Kong
 * @date: 2017年9月12日 下午14:08:45
 * 
 */
@Controller
@RequestMapping("/device")
public class DeviceController {

	@Resource
	IDeviceService deviceService;
	/**
	 * 新增设备
	 * @param device
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST, consumes = "application/json")
	public @ResponseBody Map<String, Object> addDevice(@RequestBody JSONObject device) {
		int storeId = device.getIntValue("storeId");
		String deviceName = device.getString("deviceName");
		String ipcId = device.getString("ipcId");
		String ip = device.getString("ip");
		int port = device.getIntValue("port");
		return deviceService.addDevice(storeId, deviceName, ipcId, ip, port);
	}
	/**
	 * 修改设备信息
	 * @param device
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST, consumes = "application/json")
	public @ResponseBody Map<String, Object> updateDevice(@RequestBody JSONObject device) {
		int deviceId = device.getIntValue("deviceId");
		int storeId = device.getIntValue("storeId");
		String deviceName = device.getString("deviceName");
		String ipcId = device.getString("ipcId");
		String ip = device.getString("ip");
		int port = device.getIntValue("port");
		return deviceService.updateDevice(deviceId, storeId, deviceName, ipcId, ip, port);
	}
	/**
	 * 查询设备编号
	 * @param page
	 * @param size
	 * @param ipcId
	 * @return
	 */
	@RequestMapping(value="/selectIpcId",method=RequestMethod.GET)
	public @ResponseBody String selectIpcId(@RequestParam(value="page",defaultValue="1")int page,
			@RequestParam(value="size",defaultValue="20")int size,
			@RequestParam(value="ipcId",defaultValue="")String ipcId) {
		return deviceService.selectIpcId(page, size, ipcId);
	}
	/**
	 * 查询设备信息
	 * @param page
	 * @param size
	 * @param ipcId
	 * @param deviceName
	 * @param storeName
	 * @return
	 */
	@RequestMapping(value="/selectDevice",method=RequestMethod.GET)
	public @ResponseBody String selectDevice(@RequestParam(value="page",defaultValue="1")int page,
			@RequestParam(value="size",defaultValue="20")int size,
			@RequestParam(value="ipcId",defaultValue="")String ipcId,
			@RequestParam(value="deviceName",defaultValue="")String deviceName,
			@RequestParam(value="storeName",defaultValue="")String storeName) {
		return deviceService.selectDevice(page, size, deviceName, ipcId, storeName);
	}
	/**
	 * 查询设备编号和门店信息
	 * @return
	 */
	@RequestMapping(value="/select",method=RequestMethod.GET)
	public @ResponseBody Map<String, Object> select(){
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("start", 0);
		param.put("size", 10);
		return deviceService.select(param);
	}
}
